import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MahjongOCR {
    public static void main(String[] args) {
        File imageFile = new File("D:\\zmq\\work-software\\dynamic_datasource\\src\\main\\resources\\tess4j\\split\\01cb692734973ef7ef61f33a01b44fa0.jpg"); // 替换为实际的图片路径

        Tesseract tesseract = new Tesseract();
        try {
            // 设置tessdata路径，这里假设tessdata在项目的resources/tessdata目录下
            tesseract.setDatapath("D:\\zmq\\work-software\\dynamic_datasource\\src\\main\\resources\\tess4j");
            // 设置识别语言为中文，你可以根据需要修改
            tesseract.setLanguage("chi_sim+eng");
            String result = tesseract.doOCR(imageFile);

            // 使用正则表达式提取自摸、吃胡、点炮数据
            Pattern selfDrawPattern = Pattern.compile("自摸(\\d+)次");
            Pattern eatHuPattern = Pattern.compile("吃胡(\\d+)次");
            Pattern fireHuPattern = Pattern.compile("点炮(\\d+)次");

            Matcher selfDrawMatcher = selfDrawPattern.matcher(result);
            Matcher eatHuMatcher = eatHuPattern.matcher(result);
            Matcher fireHuMatcher = fireHuPattern.matcher(result);

            int selfDrawCount = 0;
            int eatHuCount = 0;
            int fireHuCount = 0;

            if (selfDrawMatcher.find()) {
                selfDrawCount = Integer.parseInt(selfDrawMatcher.group(1));
            }
            if (eatHuMatcher.find()) {
                eatHuCount = Integer.parseInt(eatHuMatcher.group(1));
            }
            if (fireHuMatcher.find()) {
                fireHuCount = Integer.parseInt(fireHuMatcher.group(1));
            }

            System.out.println("自摸次数: " + selfDrawCount);
            System.out.println("吃胡次数: " + eatHuCount);
            System.out.println("点炮次数: " + fireHuCount);

        } catch (TesseractException e) {
            e.printStackTrace();
        }
    }
}